
class JQProductCreateService extends global.JQService {
	

	
    async do_action(){
		
		console.log('product create 33311111');
		if(!this.conn)console.log('do_action::conn error');
		let data_obj = this.req_obj.data_obj;
		let inst = global.JQTableIF.instance('cp_table',this.conn);
		
		if(!data_obj || !data_obj || !data_obj.obj.menu_id){
			throw {'code':global.consts.Error.ERROR_PARAM,
				'message':global.consts.Message.ERROR_PARAM};
		}
		
		let obj = data_obj.obj;
		
		if(!obj.cp_id){
			let params=super._addParams(['menu_id=',obj.menu_id]);
			let one = await inst.queryOne(['max(sort) sort'],params,null,null);
			let sort = 1;
			if(one && one.sort)sort = one.sort+1;
			obj.sort=sort;	
			obj.cp_number='xds_' + new Date().getTime();
			obj.cp_state=1;
			obj.cp_flag=0;
			obj.cp_no=sort%10000;
			obj.cp_remark_type=1;
		}
		
		
		const first_letters = this._extractInitials(obj.cp_name); // 直接取每个拼音数组的第一个元素（即声母）
		console.log('pingying ' + first_letters);
		 obj.cp_spell= first_letters;
		 let assign = {}
		 if(obj.cp_id){
			 if(obj.cp_total)
				assign['cp_total'] = `cp_total+ ${obj.cp_total}`;
			 delete obj.cp_total;
		 }
		
		let result = await inst.create(obj,assign);	
		
		if(data_obj.attrs){
			let inst_attr = global.JQTableIF.instance('cp_attribution_table',this.conn);
			let sort = data_obj.attrs.length;
			for(let i=0;i<data_obj.attrs.length;i++){
				let item = data_obj.attrs[i];
				item.cp_id=result.cp_id;
				item.sort=sort;
				sort--;
				await inst_attr.create(item);				
			}
			
		}
		if(data_obj.attr_prices){
			let inst_attr_prices = global.JQTableIF.instance('cp_attr_price_table',this.conn);
			for(let i=0;i<data_obj.attr_prices.length;i++){
				let item = data_obj.attr_prices[i];
				item.cp_id=result.cp_id;
				let on_dup={price:item.price,vip_price:item.vip_price,cp_id:result.cp_id}
				await inst_attr_prices.create(item,null,on_dup);
			};			
		}
		
		if(!result)return null;
		let wrap ={obj:result};		
		return wrap;			
	}

}
module.exports = JQProductCreateService;

